package com.wy.sloth.core;

import static com.wy.sloth.constant.SlothConstants.REDIS_NAMESPACE_SEPARATOR;
import static com.wy.sloth.constant.SlothConstants.SLOTH;

import com.wy.sloth.config.SlothConfig;
import com.wy.sloth.util.UUIDUtils;

/**
 * 项目默认的名称生成器
 * 
 * @author wangyuan
 * @since 2019年1月31日
 */
public class SlothStandardNameManager implements SlothNameManager {
	
	private SlothConfig slothConfig;
	
	public SlothStandardNameManager(SlothConfig slothConfig) {
		this.slothConfig = slothConfig;
	}

    @Override
    public String getApplicationQueueKey() {
        StringBuilder sb = new StringBuilder();
        sb.append(getNameSpace());
        sb.append("mq" + REDIS_NAMESPACE_SEPARATOR);
        sb.append(slothConfig.getServiceIp() + REDIS_NAMESPACE_SEPARATOR);
        sb.append(slothConfig.getServicePort() + REDIS_NAMESPACE_SEPARATOR);
        sb.append(slothConfig.getServiceName());
        
        return sb.toString();
    }

	@Override
	public String getType2QueueKey() {
		return getNameSpace() + "mapping:type2Queue";
	}

	@Override
	public String getMsgLogKey() {
		return getNameSpace() + "log:msg";
	}

	@Override
	public String getDealStatus2MsgIdAndQueueKey(int dealStatus) {
		StringBuilder sb = new StringBuilder();
        sb.append(getNameSpace());
        sb.append("log:dealStatus" + REDIS_NAMESPACE_SEPARATOR);
        sb.append(dealStatus);
        
		return sb.toString();
	}

	@Override
	public String getDealResultKey(String msgId, String queueName) {
		return getDealResultKeyPrefix(msgId) + queueName;
	}

	@Override
	public String getDealResultKey(String keySuffix) {
		return getDealResultKeyPrefix() + keySuffix;
	}

	@Override
	public String getMsg2CreatedTimeKey() {
		return getNameSpace() + "log:createdTime";
	}

	@Override
	public String getClearLogWorkerFlagKey() {
		return getNameSpace() + "flag:clearLogWorker";
	}

	@Override
	public String getDealResultKeyPrefix(String msgId) {
		return getDealResultKeyPrefix() + msgId + REDIS_NAMESPACE_SEPARATOR;
	}

	@Override
	public String getDealResultKeyPrefix() {
		return getNameSpace() + "log:dealResult" + REDIS_NAMESPACE_SEPARATOR;
	}

	@Override
	public String getTempKey() {
		return getNameSpace() + "temp" + REDIS_NAMESPACE_SEPARATOR + UUIDUtils.generateUUID();
	}
	
	@Override
	public String getMsgReceiptKey() {
		return getNameSpace() + "msg:receipt";
	}
	
	@Override
    public String getLockKey(String needLockKey) {
        return getNameSpace() + "lock:" + needLockKey;
    }

	private String getNameSpace() {
		StringBuilder sb = new StringBuilder();
        sb.append(SLOTH + REDIS_NAMESPACE_SEPARATOR);
        sb.append(slothConfig.getGroup() + REDIS_NAMESPACE_SEPARATOR);
        
        return sb.toString();
	}
    
}
